package com.example.demo.servicer;

import com.example.demo.mapper.BookNapper;
import com.example.demo.model.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
@Slf4j
@Service
public class BookUservice {
    @Autowired
    public BookNapper bookNapper;
    @Autowired
    public PageRequest pageRequest;
    public PageResult getBookList(PageRequest pageRequest){

     List<BookInfo> list=bookNapper.getBookInfo(pageRequest.getOffset(),pageRequest.getPageSize());
     int count=bookNapper.getBookInfoCount();
     for(BookInfo bookInfo:list){
        int tem= bookInfo.getStatus();
        if(tem==1){
            bookInfo.setStatusCN("可以借阅");

        }
        if(tem==2){
            bookInfo.setStatusCN("不可借阅");
        }
     }
     return new PageResult<>(list,count,pageRequest);

    }
    public int updateBookInfo(BookInfo bookInfo){
        int result=0;


            result=bookNapper.updateBookInfo(bookInfo);

        return result;

    }

    public BookInfo getBookInfoById(int id){
        BookInfo bookInfo=null;
      try {
          bookInfo=bookNapper.getBookInfoById(id);
      }catch (Exception e){
          log.error(e.getMessage());
      }
        return bookInfo;
    }
    public int insertBookInfo(BookInfo bookInfo){
        int result=0;
        try {
            result=bookNapper.insertBookInfo(bookInfo);
        }catch (Exception e){
            log.error(e.getMessage());
        }

        return result;
    }
    public int deleteBookInfoById(Integer id){
        int result=0;
        try {
            result=bookNapper.deleteBookInfoById(id);
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return result;
    }
    public boolean updateBookInfoById(BookInfo bookInfo){
    int a=   bookNapper.updateBookInfo(bookInfo);
if(a==1)  return true;
return false;

    }
    public int batch(List<Integer> ids){
        return bookNapper.batch(ids);
    }
//    @RequestMapping(value = "/batchDelete", produces = "application/json")
//    public String batchDelete(@RequestParam List<Integer> ids){
//        log.info("接收请求, 批量删除图书, 图书ID:{}",ids);
//        Integer result = bookService.batchDelete(ids);
//        if (result<=0){
//            log.error("批量删除失败, ids:{}",ids);
//            return "批量删除失败, 请联系管理员";
//        }
//        return "";
//    }
}
